package dvsdk.util;

import db.BeanTransBase;
import db.BeanTransFullTable;
import db.BeanTransParam;
import db.BeanTransRecord;
import db.BeanTransTable;
import dvsdk.GlobalConfig;
import dvsdk.db.Base;
import dvsdk.db.BeanBase;
import dvsdk.db.BeanParam;
import dvsdk.db.BeanUser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import usr.BeanTransAgent;
import usr.BeanTransUser;

/**
 *
 * @author Develop
 */
public class UtilWeb {
    public static BeanTransAgent getTransportAgent() {
        BeanTransAgent bta = new BeanTransAgent();
        bta.setIda(GlobalConfig.MASTER_INN_GLOBAL);
        bta.setInfo(GlobalConfig.MASTER_INFO);
        bta.setEmail(GlobalConfig.MASTER_EMAIL);
        bta.setName(GlobalConfig.MASTER_NAME);
        bta.setSite(GlobalConfig.MASTER_SITE);
        bta.setHide(GlobalConfig.MASTER_EMAIL_HIDE);

        List<BeanTransUser> users = new ArrayList<BeanTransUser>();
        for (Iterator<BeanUser> it = GlobalConfig.LIST_USERS.iterator(); it.hasNext();) {
            BeanUser beanUser = it.next();
            users.add(new BeanTransUser(beanUser.getIdr(), beanUser.getName(), beanUser.getPwdMD5(), beanUser.getContact()));
        }
        bta.setAgents(users);
        return bta;
    }
    
    @Deprecated
    public static BeanTransTable getTransportTable(Base base, String table, String where, String ida, String idr) {
        BeanTransTable btt = new BeanTransTable(ida, idr, table);
        List<BeanBase> beanBase = new ArrayList<BeanBase>();
        List<BeanTransRecord> records = new ArrayList<BeanTransRecord>();
        beanBase=ExUtil.selectSE(base, table, "", where);
        for (Iterator<BeanBase> it = beanBase.iterator(); it.hasNext();) {
            BeanBase beanBase1 = it.next();
            records.add(new BeanTransRecord(beanBase1.getId(), beanBase1.getDate_mod()));
        }
        btt.setRecords(records);
        return btt;
    }
    
    public static BeanTransFullTable getTransportTableFull(Base base, String table, String where, String fields, String ida, String idr) {
        BeanTransFullTable btt = new BeanTransFullTable(ida, idr, table);
        List<BeanBase> beanBase = new ArrayList<BeanBase>();
        List<BeanTransBase> records = new ArrayList<BeanTransBase>();
        beanBase=ExUtil.selectSE(base, table, fields, where);
        for (Iterator<BeanBase> it = beanBase.iterator(); it.hasNext();) {
            BeanBase beanBase1 = it.next();
            BeanTransBase btr = new BeanTransBase(beanBase1.getId(), GlobalConfig.MASTER_INN_GLOBAL, beanBase1.getIdr(), beanBase1.getDate_add(), beanBase1.getDate_mod(),beanBase1.getState());
            List<BeanTransParam> params = new ArrayList<BeanTransParam>();
            for (Iterator<BeanParam> it1 = beanBase1.getParams().iterator(); it1.hasNext();) {
                BeanParam beanParam = it1.next();
                params.add(new BeanTransParam(beanParam.getName(), beanParam.getValue()));
            }
            btr.setParams(params);
            records.add(btr);
        }
        btt.setRecordsFull(records);
        return btt;
    }
    
    public static void syncTransportTable(Base base, BeanTransFullTable fullTable) {
        List<BeanTransBase> recordsFull = fullTable.getRecordsFull();
        List<BeanBase> bases = new ArrayList<BeanBase>();
        for (Iterator<BeanTransBase> it = recordsFull.iterator(); it.hasNext();) {
            BeanTransBase beanTransBase = it.next();
            BeanBase beanBase = new BeanBase(beanTransBase.getId(), beanTransBase.getIda(), beanTransBase.getIdr(), beanTransBase.getDate_add(), beanTransBase.getDate_mod(), beanTransBase.getState());
            List<BeanParam> params = new ArrayList<BeanParam>();
            for (Iterator<BeanTransParam> it1 = beanTransBase.getParams().iterator(); it1.hasNext();) {
                BeanTransParam beanTransParam = it1.next();
                params.add(new BeanParam(beanTransParam.getName(), beanTransParam.getValue()));
            }
            beanBase.setParams(params);
            bases.add(beanBase);
        }
        base.syncTransElements(fullTable.getTable(), bases);
    }
}
